﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BusinessLogic;

namespace AccountingGui.Views
{
    public partial class OrderListForm : Form
    {
        private WGRUSEntities context = new WGRUSEntities();
        private int customerId = -1;

        public OrderListForm()
        {
            InitializeComponent();
        }

        public OrderListForm(int customerId)
        {
            this.customerId = customerId;
            InitializeComponent();
        }

        private void OrderListForm_Load(object sender, EventArgs e)
        {
            if (customerId >= 0)
            {
                orderBindingSource.DataSource =
                    context.Orders.Where(o => o.KundeNr == customerId).OrderBy(o => o.OrdreId).ToList();
            }
            else
            {
                orderBindingSource.DataSource =
                       context.Orders.OrderBy(o => o.OrdreId).ToList();   
            }
        }

        private void orderBindingSource_CurrentChanged(object sender, EventArgs e)
        {
            Order order = orderBindingSource.Current as Order;
            paymentBindingSource.DataSource = order.Payments.OrderBy(o => o.PaymentDate);
            orderLineBindingSource.DataSource = order.OrderLines.OrderBy(o => o.Id);
        }

        private void bt_SendInvoice_Click(object sender, EventArgs e)
        {
            Order order = orderBindingSource.Current as Order;
            if (order == null)
            {
                MessageBox.Show("Vælg ordre først", "Fejl", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (order.OrderStateId != (int) OrderStateEnum.Shipped)
            {
                MessageBox.Show("Ordren er ikke afsendt", "Fejl", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            Facade.Instance.SendInvoice(order.OrdreId);

        }
    }
}
